<?php

namespace app\manystore\model;

use think\Model;


class CampusMallGoods extends Model
{

    

    

    // 表名
    protected $name = 'campus_mall_goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text',
        'freight_type_text',
        'free_shipping_text',
        'stock_way_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getFreightTypeList()
    {
        return ['1' => __('Freight_type 1'), '2' => __('Freight_type 2'), '3' => __('Freight_type 3')];
    }

    public function getFreeShippingList()
    {
        return ['0' => __('Free_shipping 0'), '1' => __('Free_shipping 1')];
    }

    public function getStockWayList()
    {
        return ['0' => __('Stock_way 0'), '1' => __('Stock_way 1')];
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getFreightTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['freight_type']) ? $data['freight_type'] : '');
        $list = $this->getFreightTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getFreeShippingTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['free_shipping']) ? $data['free_shipping'] : '');
        $list = $this->getFreeShippingList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStockWayTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['stock_way']) ? $data['stock_way'] : '');
        $list = $this->getStockWayList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function campusmallgoodscategory()
    {
        return $this->belongsTo('CampusMallGoodsCategory', 'c_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getIsHotList()
    {
        return ['0' => __('Is_hot 0'),'1' => __('Is_hot 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'),'1' => __('Status 1')];
    }
}
